package hasthi.tools.analyze;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;

public class CleanDataSet {
public static void main(String[] args) throws Exception {
    File dir = new File("/u/hperera/temp/cgl_data/");
    String[] keys = new String[]{"rhbe2e","rregc","mngregc","rcontrol","syscontrol","samhbcheck"};
    handleDir(dir,keys);
    
    
}

public static void handleDir(File dir,String[] keys) throws Exception{
    File outallfile = new File(dir,"all.outall");
    for(File file:dir.listFiles()){
        if(file.isDirectory()){
            if(file.getAbsolutePath().contains("cglMng_")){
                handleDir(file,keys);    
            }
        }else{
            if(file.getAbsolutePath().endsWith(".out")){
                cleanFile(file.getAbsolutePath(), keys,outallfile.getAbsolutePath());    
            }
        }
    }
}


public static void cleanFile(String fileName, String[] keys,String outfile) throws Exception{
    Writer out = new FileWriter(outfile,true);
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String line = br.readLine();
    while (line != null) {
        line = line.trim();
        
        String gkey = null;
        int count = 0;
        for(String key:keys){
            if(line.contains(key)){
                count++;
                gkey = key;
            }
        }
        if(count == 0){
            //nothing to do
        }else if(count > 1){
            //System.out.println("mixed "+ line);
            throw new Exception("mixed "+ line);
        }else{
            line = line.replaceAll("\\].*?DATA\\[", " ");
            line = line.replaceAll("[^0-9.\\s]+", " ");
            out.write(new StringBuffer().append("[").append(gkey).append("]").append("20/20 DATA[").toString());
            out.write(line);
            out.write("]\n");
        }
        line = br.readLine();
    }
    System.out.println("Done "+ fileName);
    br.close();
    out.close();
    }
     
}
